package com.usercar.controller;

import com.dk.bean.Result;
import com.dk.em.ResultEnum;
import com.usercar.pojo.CarPrice;
import com.usercar.pojo.Displacement;
import com.usercar.pojo.Mileage;
import com.usercar.pojo.Years;
import com.usercar.service.ICarPriceService;
import com.usercar.service.IDisplacementService;
import com.usercar.service.IMileageService;
import com.usercar.service.IYearsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RequestMapping("evaluationController")
@RestController
@Api("车辆估计及置换接口")
public class EvaluationController {

    @Autowired
    private IMileageService iMileageService;

    @Autowired
    private IDisplacementService iDisplacementService;

    @Autowired
    private IYearsService iYearsService;

    @Autowired
    private ICarPriceService iCarPriceService;

    @ApiOperation("车辆估价")
    @GetMapping("{pipai}/{chexi}/{nianfen}/{kuanshi}/{time}/{licheng}/{color}/{type}/{ifelse}")
    //根据车辆信息查询价格数据库的价格
    public Result getEvaluationPrice(@PathVariable("pipai")String pipai,@PathVariable("chexi")String chexi,@PathVariable("nianfen")String nianfen,@PathVariable("kuanshi")String kuanshi,@PathVariable("ifelse") Integer ifelse,@PathVariable("color")String color,@PathVariable("time")Integer time,@PathVariable("licheng")Integer licheng,@PathVariable("type")String type){

        CarPrice  carPrice = iCarPriceService.selectByMessage(pipai,chexi,nianfen,kuanshi);
        //固定资产 15%-折旧85%
        BigDecimal b = carPrice.getPrice();
        BigDecimal b1 = new BigDecimal(0.85);
        BigDecimal zhejiu = b.multiply(b1);
        //年限 权重30%
        Years y = iYearsService.selectByYears(time);
        Double year = (y.getZhejiu()/100)*0.2;
        //排气量 权重20%
        Displacement lc = iDisplacementService.selectByType(type);
        Double num = (lc.getZhejiu()/100)*0.3;
        //颜色 权重 10%
        String[] arr = {"黑色","白色","银色"};
        List<String> resultList= new ArrayList<>(Arrays.asList(arr));
        Double c = null;
        if(resultList.contains(color)){
            c=0.4*0.1;
        }else {
            c= 0.8*0.1;
        }
        //是否有火烧/水泡、运营历史 权重10%
        Double hsy = 0.0;
        if(ifelse ==1){
            hsy = 0.6 * 0.1;
        }
        //里程数 权重 30 %
        Mileage m = iMileageService.selectByMileage(licheng);
        Double m1 = (m.getZhejiu()/100)*0.3;
        Double sum = m1+c+num+year;
        BigDecimal bsum = new BigDecimal(sum);
        BigDecimal multiply = zhejiu.multiply(bsum);
        BigDecimal ttl = b.subtract(multiply);
        DecimalFormat df = new DecimalFormat("0.00");
        String format = df.format(ttl);
        return Result.buildResult(ResultEnum.SUCCESS).setData(format);
    }

}
